package com.tencent.mm.ui.mogic;

import android.content.Context;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.animation.Interpolator;

import androidx.core.widget.ScrollerCompat;

import com.tencent.mm.sdk.platformtools.ad;

import java.util.Arrays;

public class MogicA {
    public int TN;
    public int[] TK;
    public float[] TG;
    public float[] TH;
    public float[] TI;
    public float[] TJ;
    private int[] TL;
    private int[] TM;
    public int TF;
    public final ViewGroup TV;
    public final a FzJ;
    public float TO;
    public float TP;
    private int TQ;
    public ScrollerCompat lAx;
    public int mActivePointerId = -1;
    private int mTouchSlop;
    public VelocityTracker mVelocityTracker;
    public int TR;
    public View TT;
    public boolean TU;

    public final Runnable TW = new Runnable() {
        public final void run() {
            bf(0);
        }
    };

    public static abstract class a {
        public abstract boolean XL(int i);

        public void M(int i) {
        }

        public void js(int i, int i2) {
        }

        public void a(View view, float f, float f2) {
        }

        public int eMO() {
            return 0;
        }

        public int e(View view, int i) {
            return 0;
        }
    }

    private MogicA(Context context, ViewGroup viewGroup, a aVar, Interpolator interpolator) {
        IllegalArgumentException illegalArgumentException;
        if (viewGroup == null) {
            illegalArgumentException = new IllegalArgumentException("Parent view may not be null");
            throw illegalArgumentException;
        } else if (aVar == null) {
            illegalArgumentException = new IllegalArgumentException("Callback may not be null");
            throw illegalArgumentException;
        } else {
            this.TV = viewGroup;
            this.FzJ = aVar;
            ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
            this.TQ = (int) ((context.getResources().getDisplayMetrics().density * 20.0f) + 0.5f);
            this.mTouchSlop = viewConfiguration.getScaledTouchSlop();
            this.TO = (float) viewConfiguration.getScaledMaximumFlingVelocity();
            this.TP = (float) viewConfiguration.getScaledMinimumFlingVelocity();
            this.lAx = ScrollerCompat.create(context, interpolator);
        }
    }

    public final View C(int i, int i2) {
        int childCount = this.TV.getChildCount() - 1;
        while (childCount >= 0) {
            View childAt = this.TV.getChildAt(childCount);
            if (i < childAt.getLeft() || i >= childAt.getRight() || i2 < childAt.getTop() || i2 >= childAt.getBottom()) {
                childCount--;
            } else {
                return childAt;
            }
        }
        return null;
    }

    public final void cancel() {
        this.mActivePointerId = -1;
        if (this.TG != null) {
            Arrays.fill(this.TG, 0.0f);
            Arrays.fill(this.TH, 0.0f);
            Arrays.fill(this.TI, 0.0f);
            Arrays.fill(this.TJ, 0.0f);
            Arrays.fill(this.TK, 0);
            Arrays.fill(this.TL, 0);
            Arrays.fill(this.TM, 0);
            this.TN = 0;
        }
        if (this.mVelocityTracker != null) {
            this.mVelocityTracker.recycle();
            this.mVelocityTracker = null;
        }
    }

    final void bf(int i) {
        if (this.TF != i) {
            this.TF = i;
            this.FzJ.M(i);
            if (i == 0) {
                this.TT = null;
            }
        }
    }

    public final boolean z(View view, int i) {
        if (view == this.TT && this.mActivePointerId == i) {
            return true;
        } else if (view == null || !this.FzJ.XL(i)) {
            return false;
        } else {
            this.mActivePointerId = i;
            if (view.getParent() != this.TV) {
                throw new IllegalArgumentException("captureChildView: parameter must be a descendant of the ViewDragHelper's tracked parent view (" + this.TV + ")");
            }
            this.TT = view;
            this.mActivePointerId = i;
            bf(1);
            return true;
        }
    }

    private static float g(float f, float f2, float f3) {
        float abs = Math.abs(f);
        if (abs < f2) {
            return 0.0f;
        } else if (abs <= f3) {
            return f;
        } else if (f > 0.0f) {
            return f3;
        } else {
            f3 = -f3;
            return f3;
        }
    }

    public final void fH() {
        this.mVelocityTracker.computeCurrentVelocity(1000, this.TO);
        v(g(this.mVelocityTracker.getXVelocity(this.mActivePointerId), this.TP, this.TO), g(this.mVelocityTracker.getYVelocity(this.mActivePointerId), this.TP, this.TO));
    }

    public final void v(float f, float f2) {
        this.TU = true;
        this.FzJ.a(this.TT, f, f2);
        this.TU = false;
        if (this.TF == 1) {
            bf(0);
        }
    }

    public final void a(float f, float f2, int i) {
        int i2 = 0;
        if (this.TG == null || this.TG.length <= i) {
            float[] obj = new float[(i + 1)];
            float[] obj2 = new float[(i + 1)];
            float[] obj3 = new float[(i + 1)];
            float[] obj4 = new float[(i + 1)];
            int[] obj5 = new int[(i + 1)];
            int[] obj6 = new int[(i + 1)];
            int[] obj7 = new int[(i + 1)];
            if (this.TG != null) {
                System.arraycopy(this.TG, 0, obj, 0, this.TG.length);
                System.arraycopy(this.TH, 0, obj2, 0, this.TH.length);
                System.arraycopy(this.TI, 0, obj3, 0, this.TI.length);
                System.arraycopy(this.TJ, 0, obj4, 0, this.TJ.length);
                System.arraycopy(this.TK, 0, obj5, 0, this.TK.length);
                System.arraycopy(this.TL, 0, obj6, 0, this.TL.length);
                System.arraycopy(this.TM, 0, obj7, 0, this.TM.length);
            }
            this.TG = obj;
            this.TH = obj2;
            this.TI = obj3;
            this.TJ = obj4;
            this.TK = obj5;
            this.TL = obj6;
            this.TM = obj7;
        }
        float[] fArr = this.TG;
        this.TI[i] = f;
        fArr[i] = f;
        fArr = this.TH;
        this.TJ[i] = f2;
        fArr[i] = f2;
        int[] iArr = this.TK;
        int i3 = (int) f;
        int i4 = (int) f2;
        if (i3 < this.TV.getLeft() + this.TQ) {
            i2 = 1;
        }
        if (i4 < this.TV.getTop() + this.TQ) {
            i2 |= 4;
        }
        if (i3 > this.TV.getRight() - this.TQ) {
            i2 |= 2;
        }
        if (i4 > this.TV.getBottom() - this.TQ) {
            i2 |= 8;
        }
        iArr[i] = i2;
        this.TN |= 1 << i;
    }

    private boolean a(float f, float f2, int i, int i2) {
        float abs = Math.abs(f);
        float abs2 = Math.abs(f2);
        if ((this.TK[i] & i2) != i2 || (this.TR & i2) == 0 || (this.TM[i] & i2) == i2 || (this.TL[i] & i2) == i2 || (abs <= ((float) this.mTouchSlop) && abs2 <= ((float) this.mTouchSlop))) {
            return false;
        } else if ((this.TL[i] & i2) != 0 || abs <= ((float) this.mTouchSlop)) {
            return false;
        } else {
            return true;
        }
    }

    public final void f(MotionEvent motionEvent) {
        int pointerCount = motionEvent.getPointerCount();
        for (int i = 0; i < pointerCount; i++) {
            int pointerId = motionEvent.getPointerId(i);
            float x = motionEvent.getX(i);
            float y = motionEvent.getY(i);
            this.TI[pointerId] = x;
            this.TJ[pointerId] = y;
        }
    }

    public final void b(float f, float f2, int i) {
        int i2 = 1;
        if (!a(f, f2, i, 1)) {
            i2 = 0;
        }
        if (a(f2, f, i, 4)) {
            i2 |= 4;
        }
        if (a(f, f2, i, 2)) {
            i2 |= 2;
        }
        if (a(f2, f, i, 8)) {
            i2 |= 8;
        }
        if (i2 != 0) {
            int[] iArr = this.TL;
            iArr[i] = i2 | iArr[i];
        }
    }

    public final boolean b(int i, int i2, int i3, int i4, long j) {
        int left = this.TT.getLeft();
        int top = this.TT.getTop();
        int i5 = i - left;
        int i6 = i2 - top;
        if (i5 == 0 && i6 == 0) {
            this.lAx.abortAnimation();
            bf(0);
            return false;
        }
        if (j == 0) {
            j = (long) P(i5, i6, i3, i4);
        }
        ad.d("WxViewDragHelper", "ashutest:: xvel %d, yvel %d, dx %d, dy %d duration %d", i3, i4, i5, i6, j);
        this.lAx.startScroll(left, top, i5, i6, (int) j);
        bf(2);
        return true;
    }

    private int P(int i, int i2, int i3, int i4) {
        int i5 = i(i3, (int) this.TP, (int) this.TO);
        int i6 = i(i4, (int) this.TP, (int) this.TO);
        int abs = Math.abs(i);
        int abs2 = Math.abs(i2);
        int abs3 = Math.abs(i5);
        int abs4 = Math.abs(i6);
        int i7 = abs3 + abs4;
        int i8 = abs + abs2;
        abs = (int) (((i6 != 0 ? ((float) abs4) / ((float) i7) : ((float) abs2) / ((float) i8)) * ((float) h(i2, i6, 0))) + ((i5 != 0 ? ((float) abs3) / ((float) i7) : ((float) abs) / ((float) i8)) * ((float) h(i, i5, this.FzJ.eMO()))));
        return abs;
    }


    private int h(int i, int i2, int i3) {
        if (i == 0) {
            return 0;
        }
        int width = this.TV.getWidth();
        int i4 = width / 2;
        float distanceInfluenceForSnapDuration = (distanceInfluenceForSnapDuration(Math.min(1.0f, ((float) Math.abs(i)) / ((float) width))) * ((float) i4)) + ((float) i4);
        i4 = Math.abs(i2);
        if (i4 > 0) {
            width = Math.round(Math.abs(distanceInfluenceForSnapDuration / ((float) i4)) * 1000.0f) * 4;
        } else {
            width = (int) (((((float) Math.abs(i)) / ((float) i3)) + 1.0f) * 256.0f);
        }
        width = Math.min(width, 600);
        return width;
    }

    private static float distanceInfluenceForSnapDuration(float f) {
        return (float) Math.sin((float) (((double) (f - 0.5f)) * 0.4712389167638204d));
    }

    private static int i(int i, int i2, int i3) {
        int abs = Math.abs(i);
        if (abs < i2) {
            return 0;
        } else if (abs <= i3) {
            return i;
        } else if (i > 0) {
            return i3;
        } else {
            i3 = -i3;
            return i3;
        }
    }

    public final boolean u(View view, float f) {
        if (view == null) {
            return false;
        }
        if (!(this.FzJ.eMO() > 0)) {
            return false;
        } else if (Math.abs(f) > ((float) this.mTouchSlop)) {
            return true;
        } else {
            return false;
        }
    }

    public final void bd(int i) {
        if (this.TG != null) {
            this.TG[i] = 0.0f;
            this.TH[i] = 0.0f;
            this.TI[i] = 0.0f;
            this.TJ[i] = 0.0f;
            this.TK[i] = 0;
            this.TL[i] = 0;
            this.TM[i] = 0;
            this.TN &= (1 << i) ^ -1;
        }
    }

    public static MogicA a(ViewGroup viewGroup, a aVar, Interpolator interpolator) {
        return new MogicA(viewGroup.getContext(), viewGroup, aVar, interpolator);
    }
}
